package com.algorithms;

import com.algorithms.linkedlists.DoubleEndLinkedList;
import com.algorithms.linkedlists.Link;
import com.algorithms.linkedlists.LinkedList;
import com.algorithms.linkedlists.LinkedQueue;
import com.algorithms.linkedlists.SortedLinkedList;
import com.algorithms.utils.AlgorithmException;
import com.algorithms.utils.Utils;

public class test_linked_lists extends Thread {
	public void run(){
		//test_linked_list();
		//test_double_end_linkedlist();
		//test_linked_queue();
		
		test_sorted_linked_list();
	}
	private void test_sorted_linked_list(){
		SortedLinkedList<Integer> sortList = new SortedLinkedList<Integer>();
		int[] testArr = Utils.genRandomInts(10, 1000);
//		try {
			for (int i = 0; i < testArr.length; i++){
				sortList.insert(testArr[i]);
				System.out.print(testArr[i] + ",");
			}
			System.out.println();

			sortList.printElements();
			System.out.println();

//		}catch(AlgorithmException e){
//			e.printStackTrace();
//		}
	}

	
	private void test_linked_queue(){
		LinkedQueue<Integer> queue = new LinkedQueue<Integer>();
		int[] testArr = Utils.genRandomInts(10, 1000);
		try {
			for (int i = 0; i < testArr.length; i++){
				queue.push(testArr[i]);
				System.out.print(testArr[i] + ",");
			}
			System.out.println();
			
			for (int i=0; i < 7; i++){
				System.out.print(queue.pop() + ",");
			}
			int num1 = queue.pop();
			int num2 = queue.pop();
			System.out.println();
			
			queue.push(15);
			System.out.println("added 1 more");
			queue.push(52);
			System.out.println("added 2 more");
			queue.push(86);
			System.out.println("added 3 more");
			
			System.out.println("num1: " + num1 + ", num2: " + num2);
			
			// print all items
			while(!queue.isEmpty()){
				System.out.print(queue.pop() + ",");
			}
		}catch(AlgorithmException e){
			e.printStackTrace();
		}
	}

	private void test_double_end_linkedlist(){
		// randomly add data to linked list
		DoubleEndLinkedList<Integer> deLinkedList = new DoubleEndLinkedList<>();
		int[] ranInts = Utils.genRandomInts(10, 100);
		int item5 = 0, item6 = 0;
		for (int i = 0; i < ranInts.length; i++){
			deLinkedList.insertFirst(ranInts[i]);
			
			if (i == 5) item5 = ranInts[i];
			if (i == 6) item6 = ranInts[i];
			
			System.out.print(ranInts[i] + ", ");
		}
		System.out.println();
		
		// print out list
		deLinkedList.printElements();
		System.out.println();
		
		// add 2 items at last and print out again
		deLinkedList.insertLast(15);
		deLinkedList.insertLast(30);
		deLinkedList.printElements();
		System.out.println();
		
		deLinkedList.insertFirst(28);
		deLinkedList.insertFirst(90);
		deLinkedList.printElements();
		System.out.println();
	}
	
	private void test_linked_list(){
		// randomly add data to linked list
		LinkedList<Integer> linkedList = new LinkedList<>();
		int[] ranInts = Utils.genRandomInts(10, 100);
		int item5 = 0, item6 = 0;
		for (int i = 0; i < ranInts.length; i++){
			linkedList.insertFirst(ranInts[i]);
			
			if (i == 5) item5 = ranInts[i];
			if (i == 6) item6 = ranInts[i];
			
			System.out.print(ranInts[i] + ", ");
		}
		System.out.println();
		
		// print out original linked list
		linkedList.printElements();
		System.out.println();
		
		// delete a few items and print out again
		linkedList.deleteFirst();
		linkedList.deleteFirst();
		linkedList.printElements();
		System.out.println();
		
		// find element 80
		Link<Integer> found = linkedList.find(item5);
		if (found != null){
			System.out.println("found item: " + found);
			System.out.println("next item is: " + found.getNext().getContent());
		}else{
			System.err.println("item not found");
		}
		
		Link<Integer> deletedLink = linkedList.delete(item6);
		System.out.println("delete item " + item6);
		linkedList.printElements();
	}
	
	public static void main(String args[]){
		new test_linked_lists().start();
	}
}
